$(function() {
	// 打开页面输入框自动聚焦
	$("#loginName").focus();
	// 点击登录按钮时进行的判断
	$("#userLogin").on("click", function() {
		if ($("#loginName").val() == "" || $("#password").val() == "") {
			var content = "登录名或密码不能为空!";
			waring(content);
		}else {
			// 前端初步判断数据类型成功以后像后台发出请求来判断登录是否成功
			login();
		}
	});
	// 用户键盘按下enter键判断的事件
	document.onkeypress = function(event) {
		e = event ? event : (window.event ? window.event : null);
		var currKey = 0;
		currKey = e.keyCode || e.which || e.charCode;
		if (currKey == 13) {
			if ($("#loginName").val() == "" || $("#password").val() == "") {
				var content = "登录名或密码不能为空!";
				waring(content);
			} else {
				login();
			}
		}
	};
});
function login(){
	$.ajax({
		type : "POST",
		data : {
			loginName : $("#loginName").val(),
			password : $("#password").val()
		},
		url : "user/login",
		dataType : 'json',
		success : function(data) {
			var flag = data.flag;
			var content="";
			var user = "";
			if (flag == 0) {
				user=data.content;
				var userName = user.loginName;
				window.location.href = "top.html?userName=" + userName;
			} else if(flag == 10){
				var errorTime = data.errorInfo;
				var dialog = BootstrapDialog.show({
					title : '账户锁定',
					message : "您输入密码错误超过三次，此账户已锁定，请"+errorTime+"秒后再试！",
					closable: false,
					type : BootstrapDialog.TYPE_DANGER
				});
				var id = setInterval(function updateErrorTime(){
					errorTime = errorTime -1;
					if(errorTime<=0){
						clearInterval(id);
						dialog.close();
					}else{
						dialog.setMessage( "您输入密码错误超过三次，此账户已锁定，请"+errorTime+"秒后再试！");
					}
				}, 1000,dialog,errorTime,id);
			}else  {
				waring(data.errorInfo);
			} 
		},
		error: function(){
			 BootstrapDialog.alert({
				title : '提示',
				message : "连接后台服务出错，请联系管理员！",
				type : BootstrapDialog.TYPE_DANGER
			});
		}
	});
}

function waring(content) {
	 BootstrapDialog.alert({
		title : '提示',
		message : content,
		type : BootstrapDialog.TYPE_WARNING
	});
}
function info(content,data) {
	 BootstrapDialog.alert({
		title : '提示',
		message : content,
		type : BootstrapDialog.TYPE_WARNING
	});
}

function ipDef(data){
	var user = data.loginUser;
	var userName = user.loginName;
	window.location.href = "top.html?userName=" + userName;
}



